package org.ymh.unix_file_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.ymh.unix_file_system.dao.*;
import org.ymh.unix_file_system.domain.FileSystemError;
import org.ymh.unix_file_system.domain.GlobalVariable;

import java.util.HashMap;

/**
 * @author lenovo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 19:00
 */
@Controller
@RequestMapping("/showFileSystem")
public class fileSystemController {
    @Autowired
    public FileSystemOperate fileSystemOperate;
    @Autowired
    public GlobalVariable globalVariable;
    @Autowired
    public IndexOperate indexOperate;
    @Autowired
    public FoundationPropertyOperate foundationPropertyOperate;
    @Autowired
    public JsonDataOperate jsonDataOperate;
    @Autowired
    public FileSystemError error;
    @Autowired
    public DirNodeOperate dirNodeOperate;
    @GetMapping("")
    public String showRoot(Model model)
    {
        model.addAllAttributes(fileSystemOperate.JsonDate(indexOperate.getOccupyBlockByInode(globalVariable.rootInode),globalVariable.rootDirNode,globalVariable.getError()));
        return "fileSystem";
    }
    @PostMapping("/createFile")
    public String createFile(@RequestParam("fileName") String fileName,@RequestParam("fileSize") Integer fileSize,Model model)
    {
        if(Math.ceil(fileSize/1024)>globalVariable.foundation.singleFileMaxSize)
        {
            error.setError(error.OutOfSingleFileMaxSizeError);
            System.out.println("超出文件大小");
        }
        System.out.println(fileName+fileSize);
        if(fileName!=null&&!fileName.equals(""))
        {
            fileSystemOperate.createFile(fileName, Long.valueOf(fileSize));
        }
        model.addAllAttributes(fileSystemOperate.JsonDate(indexOperate.getOccupyBlockByInode(globalVariable.visitingRecord.peek().inode),
                globalVariable.visitingRecord.peek().dirNode,globalVariable.getError()));
        fileSystemOperate.saveDate();
        return "fileSystem";
    }
    @PostMapping("/createDir")
    public String createDir(@RequestParam("fileName") String fileName,Model model)
    {
        System.out.println(fileName);
        if(fileName!=null&&!fileName.equals(""))
        {
            fileSystemOperate.createDir(fileName);
        }
        model.addAllAttributes(fileSystemOperate.JsonDate(indexOperate.getOccupyBlockByInode(globalVariable.visitingRecord.peek().inode),
                globalVariable.visitingRecord.peek().dirNode,globalVariable.getError()));
        fileSystemOperate.saveDate();
        return "fileSystem";
    }
    @PostMapping("/delete")
    public String delete(@RequestParam("fileName") String fileName,Model model)
    {
        System.out.println(fileName);
        if(fileSystemOperate.deleteFile(fileName))
        {

        }
        model.addAllAttributes(fileSystemOperate.JsonDate(indexOperate.getOccupyBlockByInode(globalVariable.visitingRecord.peek().inode),
                globalVariable.visitingRecord.peek().dirNode,globalVariable.getError()));
        fileSystemOperate.saveDate();
        return "fileSystem";
    }
    @PostMapping("/open")
    public String open(@RequestParam("fileName") String fileName,Model model)
    {
        System.out.println(fileName);
        HashMap<String, Object> map = fileSystemOperate.cd(fileName);


        if(dirNodeOperate.findFileItem(globalVariable.visitingRecord.peek().dirNode,fileName)!=null)
        {
            map.put("showBlock",true);
        }
        model.addAllAttributes(map);
        return "fileSystem";
    }
    @PostMapping("/re")
    public String re(Model model)
    {
        model.addAllAttributes(fileSystemOperate.re());
        return "fileSystem";
    }
    @PostMapping("/showBlock")
    public String showBlock(@RequestParam("inodeNumber") Integer inodeNumber,Model model)
    {
        System.out.println(inodeNumber);
        HashMap<String, Object> stringObjectHashMap = fileSystemOperate.JsonDate(indexOperate.getOccupyBlockByInode(globalVariable.inodeArray[inodeNumber])
                , globalVariable.visitingRecord.peek().dirNode, globalVariable.getError());
        stringObjectHashMap.put("showBlock",true);
        model.addAllAttributes(stringObjectHashMap);
        return "fileSystem";
    }
}
